import java.util.Arrays;
import java.util.Scanner;

/**
 * 1.构建26字母的数组，分别统计字母出现的次数
 * 2.将数组进行排序
 * 3.依次与系数相乘
 */
public class Beautiful {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = Integer.parseInt(scanner.nextLine());
            for (int i = 0; i < n; i++) {
                String word = scanner.nextLine();
                System.out.println(word);
                int[] wordCount = new int[26];
                for (int j = 0; j < word.length(); j++) {
                    char c = word.charAt(j);
                    if(c>='a' && c<='z'){
                        wordCount[c-'a']++;
                    }
                }
                Arrays.sort(wordCount);
                int a = 1;
                int sum = 0;
                for (int j = 0; j < wordCount.length; j++) {
                    sum += wordCount[j]*a;
                    a++;
                }
                System.out.println(sum);
            }
        }
    }
}
